/**
 * 
 * @authors 前端小灰狼
 * @date    2020-12-10 16:48:28
 * @version v1.0
 * @description the core js of todolist project
 * 
 * ━━━━━━神兽出没━━━━━━
 * 　　   ┏┓　 ┏┓
 * 　┏━━━━┛┻━━━┛┻━━━┓
 * 　┃              ┃
 * 　┃       ━　    ┃
 * 　┃　  ┳┛ 　┗┳   ┃
 * 　┃              ┃
 * 　┃       ┻　    ┃
 * 　┃              ┃
 * 　┗━━━┓      ┏━━━┛ Code is far away from bugs with the animal protecting.
 *       ┃      ┃     神兽保佑,代码无bug。
 *       ┃      ┃
 *       ┃      ┗━━━┓
 *       ┃      　　┣┓
 *       ┃      　　┏┛
 *       ┗━┓┓┏━━┳┓┏━┛
 *     　  ┃┫┫　┃┫┫
 *     　  ┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

// 请根据考试说明文档中列出的需求进行作答
// 预祝各位顺利通过本次考试，see you next week！
// ...
const todolist = document.getElementById('todolist');
const donelist = document.getElementById('donelist');
const todoCount = document.getElementById('todocount');
const doneCount = document.getElementById('donecount');
let todoc =0;
let donec=0;
 
// 添加todo内容 判断title的值 是不是空值 是就提示不可为空 
function postAction() {
    const title = document.getElementById("title");
    if(title.value ===" "){
        alert("内容不可为空！")
    }
    // 不为空的话  执行写入li
    else{
        var li = document.createElement("li");
        li.innerHTML ='<input type="checkbox" onchange="update();">' +
            '<input class="title" type="text" onchange="change();" ">' +
            '<a href="javascript:remove();">-</a>';
        if(todoc ===0){
            todolist.appendChild(li);
        }else{
            todolist.insertBefore(li,todolist.children[0]);
        }
        var txtTitle = document.getElementsByClassName("title")[0];
        txtTitle.value = title.value;
 
        loop('todolist');
        todoc++;
        todoCount.innerText = todoc;
 
        title.value = "";
    }
}

// 循环 每次添加不同的i值
function loop(str){
    let list = null;
    // 判断todolist
    str ==='todolist' ? list = todolist :list = donelist;
    // .childnodes 获取 子节点
    childs = list.childNodes;
    for(var i=0; i<childs.length;i++){
        childs[i].children[0].setAttribute('onchange','update("'+i+'","'+str+'")');
        childs[i].children[1].setAttribute('onclick','edit("'+i+'","'+str+'")');
        childs[i].children[1].setAttribute('onchange','change("'+i+'","'+str+'","'
            +childs[i].children[1].value+'")');
        childs[i].children[2].setAttribute('href','javascript:remove("'+i+'","'+str+'")');
    }
}

// uapdata
function update(n,str){
    let list = null;
    str === 'todolist' ? list = todolist : list = donelist;
 
    let li = null;
    childs = list.childNodes;
    for(var i=0;i<childs.length;i++){
        if(i===Number(n)){
            li = childs[i];
        }
    }
    remove(n,str);
 
    if(str==='todolist'){
        if(donec ===0){
            donelist.appendChild(li);
        }else {
            donelist.insertBefore(li,donelist.children[0]);
        }
        loop('donelist');
        donec++;
        doneCount.innerText = donec;
    }else if(str ==='donelist'){
        todolist.appendChild(li);
        loop('todolist');
        todoc++;
        todoCount.innerText = todoc;
    }
}
 
function edit(n,str) {
    let list = null;
    str ==='todolist' ? list = todolist : list = donelist;
    childs = list.childNodes;
    for(var i=0;i<childs.length;i++){
        if(i===Number(n)){
            childs[i].children[1].style.border = '1px solid gray';
        }
    }
}
 
function change(n,str,oldValue) {
    let list = null;
    str==='todolist' ? list = todolist : list = donelist;
    childs = list.childNodes;
    for(var i=0; i<childs.length; i++){
        if(i===Number(n)){
            childs[i].children[1].style.border = 'none';
            if(childs[i].children[1].value === ""){
                alert('内容不能为空');
                childs[i].children[1].value = oldValue;
            }
        }
    }
    loop(str);
}

function remove(n,str) {
    let list=null;
    if (str==='todolist'){
        list = todolist;
        todoc--;
        todoCount.innerText = todoc;
    } else if(str==='donelist'){
        list = donelist;
        donec--;
        doneCount.innerText = donec;
    }
 
    childs = list.childNodes;
    for(var i=childs.length-1;i>=0;i--){
        if(i===Number(n)){
            list.removeChild(childs[n]);
        }
    }
    loop(str);
}



const ipv4 = document.getElementById('ipv4')
const addr = document.getElementById('addr')
const xhr = new XMLHttpRequest();
xhr.open('get' , '/aaa');  
xhr.onload = function(){
    const res= JSON.parse(xhr.responseText)
    ipv4.innerText=res.ip
    addr.innerText=res.area
}
xhr.send()
